﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SWCad
{
    public partial class FormHyetograph : Form
    {
        public IWin32Window pDlg;
 //       public IUHChart pDlg;
        ClassLib formClib;
        
        public FormHyetograph()
        {
            InitializeComponent();
        }

        public void ShowDialog(string customID, TLJSChart parent, ClassLib clib)
        {
            this.Text = customID + "雨型";
            pDlg = parent;

            dataGridView1.AutoGenerateColumns = false;
            formClib = clib;
            SetGridData();
            // Show the dialog
            this.ShowDialog(parent);
        }
        
        private void DataGridCellPaste()
        {
             try
            {
                // 获取剪切板的内容，并按行分割   
                string pasteText = Clipboard.GetText();
                if (string.IsNullOrEmpty(pasteText))
                    return;
                int tnum = 0;
                int nnum = 0;
                //获得当前剪贴板内容的行、列数
                for (int i = 0; i < pasteText.Length; i++)
                {
                    if (pasteText.Substring(i, 1) == "\t")
                    {
                        tnum++;
                    }
                    if (pasteText.Substring(i, 1) == "\n")
                    {
                        nnum++;
                    }
                }
                Object[,] data;
                //粘贴板上的数据来自于EXCEL时，每行末都有\n，在DATAGRIDVIEW内复制时，最后一行末没有\n
                if (pasteText.Substring(pasteText.Length - 1, 1) == "\n")
                {
                    nnum = nnum - 1;
                }
                tnum = tnum / (nnum + 1);
                data = new object[nnum + 1, tnum + 1];//定义一个二维数组
                String rowstr;
                rowstr = "";
                //MessageBox.Show(pasteText.IndexOf("B").ToString());
                //对数组赋值
                for (int i = 0; i < (nnum + 1); i++)
                {
                    for (int colIndex = 0; colIndex < (tnum + 1); colIndex++)
                    {
                        //一行中的最后一列
                        if (colIndex == tnum && pasteText.IndexOf("\r") != -1)
                        {
                            rowstr = pasteText.Substring(0, pasteText.IndexOf("\r"));
                        }
                        //最后一行的最后一列
                        if (colIndex == tnum && pasteText.IndexOf("\r") == -1)
                        {
                            rowstr = pasteText.Substring(0);
                        }
                        //其他行列
                        if (colIndex != tnum)
                        {
                            rowstr = pasteText.Substring(0, pasteText.IndexOf("\t"));
                            pasteText = pasteText.Substring(pasteText.IndexOf("\t") + 1);
                        }
                        data[i, colIndex] = rowstr;
                    }
                    //截取下一行数据
                    pasteText = pasteText.Substring(pasteText.IndexOf("\n") + 1);
                }
                //获取当前选中单元格所在的列序号
                int curntindex = dataGridView1.CurrentRow.Cells.IndexOf(dataGridView1.CurrentCell);
                //获取获取当前选中单元格所在的行序号
                int rowindex = dataGridView1.CurrentRow.Index;
                //获取当前grid最大行数
                int rowNum = dataGridView1.RowCount;
                //补齐不够的行数
                for (int j = rowNum; j < (nnum + rowindex + 1); j++)
                    dataGridView1.Rows.Add("");
                //MessageBox.Show(curntindex.ToString ());
                for (int j = 0; j < (nnum + 1); j++)
                {
                    for (int colIndex = 0; colIndex < (tnum + 1); colIndex++)
                    {
                        dataGridView1.Rows[j + rowindex].Cells[colIndex + curntindex].Value = data[j, colIndex];                        
                    }                    
                }
            }
            catch
            {
                MessageBox.Show("粘贴区域大小不一致");
                return;
            }
        }

        private void GetGridData() ///表格值传递给类变量
        {
            if( 0 == formClib.iHx.Length)
                formClib.iHx = new int[24];//定义一个数组
            try
            {
                for (Int32 i = 0; i < 24; i++)
                {
                    formClib.iHx[i] = int.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());                    
                }
                 ((TLJSChart)pDlg).clib = formClib;
            }
            catch
            {
                MessageBox.Show("雨型应有24个数据");
                return;
            }
        }
        public void SetGridData() ///变量值传给表格
        {
            try 
            {
                dataGridView1.Rows.Clear();
                dataGridView1.Rows.Add(24);
                for (int i=0; i<24; i++)
                {
                    dataGridView1.Rows[i].Cells[0].Value = i+1;
                    dataGridView1.Rows[i].Cells[1].Value = formClib.iHx[i].ToString();
                }
            }
            catch
            {
                MessageBox.Show("雨型应有24个数据");
                return;
            }
        }
        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyValue == 86) //Ctrl+V
            {
                DataGridCellPaste();
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            GetGridData();
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
